package General;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Manuel
 */
public class RequestMaker {

    public static String getPage(String u){
        long elapsedTime = System.currentTimeMillis();
        String page ="";
        boolean success = false;
        int trials = 0;
        while(!success && trials < 3){
            try {
                URL url = new URL(u);
                URLConnection uc = url.openConnection();
                uc.addRequestProperty("Host", "code.google.com");
                uc.setRequestProperty("user-agent", "Mozilla/5.0 Gecko/20110420 Firefox/3.6.17");
                BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
                String str;
                while ((str = in.readLine()) != null) {
                    page = page + str;
                }
                success = true;
            } catch (Exception ex) {
                trials++;
            }
        }
        if(!success)
            System.err.println("Cannot reach the requested page "+u+ " wrong url or missing connection");
        return page;
 }

    public static String getMap(String u){
        String result = "";
        String line;
        try {
            URL url = new URL(u);
            URLConnection uc = url.openConnection();
            BufferedReader r = new BufferedReader(new InputStreamReader(uc.getInputStream()));
            do {
                line = r.readLine();
                if (line != null) {
                    result = result + line;
                }
            } while (line != null);
        } catch (Exception ex) {
            System.err.println("Could not retrieve sitemap: "+u+" wrong url or missing connection");
            result = "";
        }
        return result;
    }



    public static String storeFile(String urlString, String destination){

        String page ="";
        String destFile = "";
        boolean success = false;
        int trials = 0;
        while(!success && trials < 3){
            try {
                  File dir = new File(destination);
                  if(!dir.exists() || !dir.isDirectory())
                      dir.mkdirs();
                  destFile = urlString;
                  while(destFile.contains("/")){
                          destFile = destFile.substring(destFile.indexOf("/")+1);
                  }
                  destFile = destination +"/"+destFile;
                  URL url  = new URL(urlString);
                  URLConnection urlC = url.openConnection();
                  // Copy resource to local file, use remote file
                  // if no local file name specified
                  InputStream is = urlC.getInputStream();
                  Date date=new Date(urlC.getLastModified());
                  File outputFile = new File(destFile);
                  FileOutputStream fos = new FileOutputStream(outputFile);
                  int oneChar, count=0;
                  while ((oneChar=is.read()) != -1){
                     fos.write(oneChar);
                     count++;
                  }
                  is.close();
                  fos.close();
                  success = true;
            } catch (Exception ex) {
                new File(destFile).delete();
                trials++;
            }
        }
        if(!success){
            System.err.println("Cannot download the requested file "+ urlString);
            return "";
        }
        return destFile;
    }




}
